//
//  LBWTabBarItem.swift
//  weibo
//
//  Created by Limonwei on 15/12/12.
//  Copyright © 2015年 heima. All rights reserved.
//

//一层一层的取到想要的控件 runTime运行方法遍历获取成员变量

import UIKit

class LBWTabBarItem: UITabBarItem {
    
    //设置badgeValue
    override var badgeValue: String? {
        
        didSet{
            
            if badgeValue == nil {
                
                return
            }
            
            let controller = valueForKeyPath("_target") as! UITabBarController
            
            for tabBarChild in controller.tabBar.subviews {
                
                //如果是tabBarButton
                if tabBarChild.isKindOfClass(NSClassFromString("UITabBarButton")!){
                    
                    //继续往下找
                    for tabBarButtonChild in tabBarChild.subviews {
                        
                        if tabBarButtonChild.isKindOfClass(NSClassFromString("_UIBadgeView")!){
                            
                            for badgeViewChild in tabBarButtonChild.subviews{
                                
                                if badgeViewChild.isKindOfClass(NSClassFromString("_UIBadgeBackground")!){
                                    
                                    printLog("找到了\(badgeViewChild)")
                                    
                                    badgeViewChild.setValue(UIImage(named: "main_badge"), forKey: "_image")
//                                    
//                                    //记录获取回来的成员变量有哪些
//                                    var count : Int32 = 0
//                                    
//                                    //通过运行参数,获取其成员的属性或成员变量
//                                    let ivars = class_copyIvarList(NSClassFromString("UIBadgeBackground")!, &count)
//                                    
//                                    //遍历成员变量的列表
//                                    for i in 0..<count {
//                                        
//                                        //取到对应位置的值
//                                        let ivar = ivars[Int(i)]
//                                        //获取其名字
//                                        let name = ivar_getName(ivar)
//                                        let type = ivar_getTypeEncoding(ivar)
//                                        
//                                        //转成NSString
//                                        let nameString = NSString(CString: name, encoding: NSUTF8StringEncoding)
//                                        let typeString = NSString(CString: type, encoding: NSUTF8StringEncoding)
//                                        
//                                        //打印出来
//                                        printLog("\(nameString)---\(typeString)")
//                                        
//                                        //判断其对应属性,通过kvc属性赋值
//                                        
//                                        if nameString!.isEqualToString("_image"){
//                                            badgeViewChild.setValue(UIImage(named: "main_badge"), forKey: (nameString as! String))
//                                        }
//                                        
//                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
        
        
    }
    

}




































